package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Friend;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @Description: test
 * @Create by: JJ菜菜
 * @Date: 2021/12/4 23:47
 */

@DubboService
public class FriendApiImpl implements FriendApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 添加好友将好友记录到friend
     * @param id
     * @param friendId
     */
    @Override
    public void save(Long id, Long friendId) {

        // 1. 保存自己的好友信息
        Query query = Query.query(Criteria.where("userId").is(id).and("friendId").is(friendId));
        boolean exists = mongoTemplate.exists(query, Friend.class);
        // 2. 判断好友关系是否存在
        if(!exists){
            // 如果不存在就保存
            Friend friend = new Friend();
            friend.setUserId(id);
            friend.setFriendId(friendId);
            friend.setCreated(System.currentTimeMillis());
            mongoTemplate.save(friend);
        }
        // 3. 保存好友的数据

        Query query1 = Query.query(Criteria.where("userId").is(friendId).and("friendId").is(id));
        boolean exists1 = mongoTemplate.exists(query1, Friend.class);
        // 3.1 判断好友关系是否存在
        if(!exists1){
            // 如果不存在就保存
            Friend friend = new Friend();
            friend.setUserId(friendId);
            friend.setFriendId(id);
            friend.setCreated(System.currentTimeMillis());
            // 如果不存在，就保存
            mongoTemplate.save(friend);
        }

    }

    /**
     * 分页查询好友泪列表
     * @param id
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public List<Friend> findByUserId(Long id, Integer page, Integer pageSize) {

        Criteria criteria = Criteria.where("userId").is(id);
        Query query = Query.query(criteria)
                .skip((page - 1) * pageSize)
                .limit(pageSize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Friend> friendList = mongoTemplate.find(query, Friend.class);
        return friendList;
    }

    @Override
    public void remove(Long id, Long friendId) {

        // 判断自己的好友信息是否存在
        Query query = Query.query(Criteria.where("userId").is(id).and("friendId").is(friendId));
        boolean exists = mongoTemplate.exists(query, Friend.class);
        if(exists){
            // 存在就删除
            mongoTemplate.remove(query);
        }

        // 判断好友关系是否存在
        Query query1 = Query.query(Criteria.where("userId").is(friendId).and("friendId").is(id));
        boolean exists1 = mongoTemplate.exists(query1, Friend.class);
        if(exists1){
            mongoTemplate.remove(query1);
        }

    }
}
